<?php
/**
 * div ที่จำเป็นต้องมี ดังนี้
 * div[id=divFirstHeader] สำหรับแสดงที่หัวหน้าแรก
 * div[class=header] ส่วนหัวที่จะปรากฏทุกหน้า
 *
 * div[main] ส่วนตารางข้อมูลที่จะแสดง
 *
 * div[main] table thead หัวตารางข้อมูล
 * div[main] table tbody รายการข้อมูล
 *
 * div[class=footer] ส่วนท้ายของแต่ละหน้า
 * div[id=divLastFooter] สำหรับแสดงที่หน้าสุดท้าย
 *
 * ******* จำเป็นมาก แม้ไม่มีข้อมูลในส่วนนั้นก็ต้องมี div เหล่านี้อยู่ เพื่อให้การแบ่งหน้าทำได้ถูกต้อง ************
 */
?>
<div id="divFirstHeader">
    <div class="title">
        <?php echo $this->element('print/title'); ?>
        <table>
            <tr>
                <td width="120px"><label>Customer no.</label></td>
                <td><?php echo $customer_no; ?></td>
            </tr>
            <tr>
                <td><label>Customer name</label></td>
                <td><?php echo $customer_name; ?></td>
            </tr>
            <tr>
                <td><label>Site</label></td>
                <td><?php echo $site_name; ?></td>
            </tr>
            <?php if (!empty($cost_center_name)) : ?>
            <tr>
                <td width="120px"><label>Cost Center</label></td>
                <td><?php echo $cost_center_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['CustStockOutHead']['stock_out_date_start'])) : ?>
                    <tr>
                        <td width="120px"><label>Stock out date</label></td>
                        <td>From <?php echo $criteria['CustStockOutHead']['stock_out_date_start']; ?> to <?php echo $criteria['CustStockOutHead']['stock_out_date_end']; ?></td>
                    </tr>
            <?php endif; ?>
        </table>
    </div>
    <div class="documentTitle">
        <div class="documentNote">
            VMI REPORT: Customer Stock out<br />
            Group by Cost Center
        </div>
        <table>
            <tr>
                <td><label>Report date</label></td>
                <td><?php echo $report_date; ?></td>
                <td><label>Report by</label></td>
                <td><?php echo $user; ?></td>
                <td><label>Page</label></td>
                <td><span class="pageNo">1/1</span></td>
            </tr>
            <tr>
                <td width="100px"><label>Vendor no.</label></td>
                <td colspan="5"><?php echo $vendor_no; ?></td>
            </tr>
            <tr>
                <td width="100px"><label>Vendor name.</label></td>
                <td colspan="5"><?php echo $vendor_name; ?></td>
            </tr>
        </table>
    </div>
</div>

<!--div class="header">
</div-->

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="no" rowspan="2">No.</th>
                <th colspan="3" style="text-align: left;">&nbsp;Cost Center</th>
                <th rowspan="2" width="40px">Freq.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th rowspan="2" width="45px">Price</th>
                <?php endif; ?>
                <th colspan="3">Quantity</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <th rowspan="2" width="50px">Amount</th>
                <?php endif; ?>
            </tr>
            <tr>
                <th width="<?php echo ($display_stock_out_no == true)? '150': '100';?>px">Stock code <?php if ($display_stock_out_no == true) echo '/ <br /> Stock out no.'; ?></th>
                <th width="<?php echo ($display_stock_out_no == true)? '140': '130';?>px">Vendor Part no.<?php if ($display_stock_out_no == true) echo '/ <br /> Stock out date'; ?></th>
                <th>Product Description <?php if ($display_stock_out_no == true) echo ' / Remark'; ?></th>
                <th width="50px">Stock out</th>
                <th width="50px">Return</th>
                <th width="50px">Net</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 0;
            $r = 0;
            $total_freq = 0;
            $total_qty = 0;
            $total_net_qty = 0;
            $total_return = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
              <tr>
                <td class="center" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $i + 1; ?></td>
                <td class="bold" colspan="3" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $data['SiteCostCenter']['name']; ?></td>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $data['SiteCostCenter']['frequency']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>">-</td>
                        <?php endif; ?>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $data['SiteCostCenter']['stock_out_qty']; ?></td>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $data['SiteCostCenter']['return_qty']; ?></td>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo $data['SiteCostCenter']['net_stock_out_qty']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="bold text-right" style="border-bottom: solid 1px; <?php echo $i != 0? "border-top: solid 1px;": ""; ?>"><?php echo number_format($data['SiteCostCenter']['amount'], 2); ?></td>
                        <?php endif; ?>
                        <?php
                        $total_freq += $data['SiteCostCenter']['frequency'];
                        $total_qty += $data['SiteCostCenter']['stock_out_qty'];
                        $total_return += $data['SiteCostCenter']['return_qty'];
                        $total_net_qty += $data['SiteCostCenter']['net_stock_out_qty'];
                        $total_amount += $data['SiteCostCenter']['amount'];
                        ?>
            </tr>
            <?php foreach($data['CustProductSite'] as $cps) : ?>
            <tr class="<?php echo ($display_stock_out_no == true? "bold": ''); ?>">
                <td class="center">&nbsp;</td>
                <td><?php echo $cps['CustProductSite']['stock_code']; ?></td>
                <td><?php echo $cps['CustStockOutHead'][0]['dispatch_from'] == ITS_PREFIX? $cps['Product']['part_no']: $cps['Product']['supplier_part_no']; ?></td>
                <td><?php echo $cps[0]['text']; ?></td>
                <td class="text-right"><?php echo $cps[0]['frequency']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'], 2); ?></td>
                        <?php endif; ?>
                <td class="text-right"><?php echo $cps[0]['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['return_qty']; ?></td>
                <td class="text-right"><?php echo $cps[0]['net_stock_out_qty']; ?></td>
                        <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($cps[0]['price'] * $cps[0]['net_stock_out_qty'], 2); ?></td>
                        <?php endif; ?>
            </tr>
                <?php if ($display_stock_out_no == true) : ?>
                    <?php
                    // ตัวแปรสำหรับลำดับเลข stock out no
                    $k = 1;
                    foreach($cps['CustStockOutHead']  as $sto) :
                        ?>
            <tr>
                <td class="rowTd">&nbsp;</td>
                            <?php if ($display_stock_out_no == true) : ?>
                <td>
                    <span><?php echo $k; ?>.</span>&nbsp;
                    <span><?php echo $sto['stock_out_no']; ?></span>
                </td>
                <td><?php echo $sto['stock_out_date']; ?></td>
                <td><?php echo $sto['remark']; ?></td>
                <td>&nbsp;</td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td>&nbsp;</td>
                                <?php endif; ?>
                <td class="text-right"><?php echo $sto['stock_out_qty']; ?></td>
                <td class="text-right"><?php echo $sto['return_qty']; ?></td>
                <td class="text-right"><?php echo $sto['net_stock_out_qty']; ?></td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right"><?php echo number_format($sto['net_stock_out_qty'] * $cps[0]['price'], 2); ?></td>
                                <?php endif; ?>
                            <?php endif; ?>
            </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php endforeach; 
            endforeach; ?>
            <tr class="trFooter">
                <td colspan="4" class="bold text-right">Grand Total</td>
                <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right">-</td>
                <?php endif; ?>
                <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
                <td class="text-right bold"><?php echo number_format($total_return); ?></td>
                <td class="text-right bold"><?php echo number_format($total_net_qty); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
                <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
                <?php endif; ?>
            </tr>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter"></div>